<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="${tablename}">

    <sql id="select_what">
        select
    <#list pkMap?keys as column>
        <#if column_index!=0>
            ,${column}
        <#else>
        ${column}
        </#if>
    </#list>
    <#list columnData?keys as column>
        ,${column}
    </#list>
        from ${tablename}
    </sql>


    <sql id="key_map">
        <dynamic prepend="where">
        <#list pkMap?keys as column>
            <isNotEmpty property="${column}" prepend=" and " removeFirstPrepend="true">
            ${column}=#${column}#
            </isNotEmpty>
        </#list>
        </dynamic>
    </sql>

    <statement id="listPK" resultClass="java.util.HashMap">
        <include refid="select_what"/>
        <include refid="key_map"/>
    </statement>

    <sql id="cond_map">
        <dynamic prepend="where">
        <#list columnData?keys as column>
            <isNotEmpty property="${column}" prepend=" and ">
            ${column}=#${column}#
            </isNotEmpty>
        </#list>
        </dynamic>
    </sql>

    <statement id="listPager" resultClass="java.util.HashMap">
        <include refid="select_what"/>
        <include refid="cond_map"/>
        limit $start$ , $pageSize$
    </statement>

    <statement id="list" resultClass="java.util.HashMap">
        <include refid="select_what"/>
        <include refid="cond_map"/>
    </statement>

    <!-- update and insert -->
    <sql id="insert_column">
        <dynamic prepend="(" close=")">
        <#list columnData?keys as column>
            <isNotEmpty property="${column}" prepend=" , ">
            ${column}
            </isNotEmpty>
        </#list>
        </dynamic>
    </sql>


    <sql id="insert_value">
        <dynamic prepend="(" close=")">
        <#list columnData?keys as column>
            <isNotEmpty property="${column}" prepend=" , ">
                #${column}#
            </isNotEmpty>
        </#list>
        </dynamic>
    </sql>


    <insert id="insert">
        insert into ${tablename}
        <include refid="insert_column"/>

        values
        <include refid="insert_value"/>
    </insert>

    <update id="update">
        update ${tablename}
        <dynamic prepend="set">
        <#list columnData?keys as column>
            <isNotEmpty property="${column}" prepend=" , ">
            ${column}=#${column}#
            </isNotEmpty>
        </#list>
        </dynamic>
        <include refid="key_map"/>
    </update>


    <delete id="delete">
        delete from ${tablename}
        <include refid="key_map"/>
    </delete>

</sqlMap>